layui.use(['form', 'jquery', 'layer'], function () {
    let form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer;   //默认启用图书

    laydate.render({
        elem: '#stockTime' //指定元素
    });

    //获取图书列表
    $.ajax({
        type: 'post',
        url: '/findallbooks?page=1&limit=1000',
        data: {},
        success: function (res) {
            let result = res.data;
            let lists = '';
            for (let i in result) {
                lists += "<option value='" + result[i].bookId + "'>" + result[i].bookName + "</option>";
            }
            $('#selectBook').html(lists);
            form.render();
        }
    });
    form.render();

    form.on("submit(addUser)", function (data) {
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });

        $.ajax({
            type: "POST",
            url: "/addBookStock",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(data.field),
            success: function (res) {
                layer.close(loadIndex);
                if (res.code === 0) {
                    parent.layer.msg("添加成功!", {time: 1500}, function () {
                        //刷新父页面
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg);
                }
            }
        });
        return false;
    });

    form.on('switch(adminUser)', function (data) {
        $("#adminUser").val(data.elem.checked);
    });

    form.on('switch(locked)', function (data) {
        $("#locked").val(data.elem.checked);
    });

});